package cn.tedu.boot031.controller;

import cn.tedu.boot031.entity.Product;
import cn.tedu.boot031.util.DBUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
//使用此注解后 相当于每一个处理请求的方法上面都添加了@ResponseBody注解
@RestController
public class ProductController {
    @RequestMapping("/insert")
    public String insert(Product product){
        //soutp 输出数据
        System.out.println("product = " + product);
        //获取数据库连接
        try (Connection conn = DBUtil.getConnection()){
            //创建执行的SQL语句
            String sql = "insert into product values(null,?,?,?)";
            //创建执行SQL语句的对象
            PreparedStatement ps = conn.prepareStatement(sql);
            //替换掉SQL语句里面的?
            ps.setString(1,product.getTitle());
            ps.setDouble(2,product.getPrice());
            ps.setInt(3,product.getNum());
            //执行SQL语句
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "添加完成!<a href='/'>返回首页</a>";
    }

    @RequestMapping("/select")
    public String select(){
        //创建一个集合 用来装商品对象
        ArrayList<Product> list = new ArrayList<>();
        //获取连接
        try (Connection conn = DBUtil.getConnection()){
            String sql = "select id,title,price,num from product";
            PreparedStatement ps = conn.prepareStatement(sql);
            //执行查询
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
                int id = rs.getInt(1);
                String title = rs.getString(2);
                double price = rs.getDouble(3);
                int num = rs.getInt(4);
                //把查询到的数据封装到对象里面
                Product p = new Product();
                p.setId(id);
                p.setTitle(title);
                p.setPrice(price);
                p.setNum(num);
                //把对象装进list集合
                list.add(p);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //把list集合里面的数据装进一个table标签里面
        String html = "<table border='1'>";
        html+="<tr><th>id</th><th>标题</th><th>价格</th><th>库存</th><th>操作</th></tr>";
        //遍历商品集合
        for (Product p : list) {
            html+="<tr>";
            html+="<td>"+p.getId()+"</td>";
            html+="<td>"+p.getTitle()+"</td>";
            html+="<td>"+p.getPrice()+"</td>";
            html+="<td>"+p.getNum()+"</td>";
            //             /delete?id=5
            html+="<td><a href='/delete?id="+p.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        //此时html里面是表格标签+数据
        return html;  // setContentType("text/html;charset=utf-8")
    }

    @RequestMapping("/delete")
    public String delete(int id){
        System.out.println("id = " + id);
        try (Connection conn = DBUtil.getConnection()){
            String sql = "delete from product where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            ps.executeUpdate();//执行SQL语句
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "删除完成!<a href='/select'>返回列表页面</a>";
    }

    @RequestMapping("/update")
    public String update(Product product){
        System.out.println("product = " + product);
        try(Connection conn = DBUtil.getConnection()) {
            String sql =
                    "update product set title=?,price=?,num=? where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,product.getTitle());
            ps.setDouble(2,product.getPrice());
            ps.setInt(3,product.getNum());
            ps.setInt(4,product.getId());
            ps.executeUpdate();//执行修改
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "修改完成!<a href='/select'>返回列表页面</a>";
    }
}
